package com.example.smartall.ui;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.smartall.R;
import com.example.smartall.adapter.SplashVPAdapter2;
import com.example.smartall.adapter.SplashVpAdapter;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.diaglog.NetSetDialog;
import com.example.smartall.fragment.SplashLoginFragment;
import com.example.smartall.fragment.SplashVpFragment;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: abbas
 * @Description: 梦开始的地方
 * <p>
 * 引导页
 * 5个页面 4个SplashVpFragment 1个SplashLoginFragment（其实5个View就好
 * 5个点 View
 * <p>
 * （1）APP启动，首先进入引导页，引导页面下方显示5个小圆圈标识，提示用户当前引导页面位置。
 * （2）进入最后引导页。
 * ①显示“网络设置”按钮，点击弹出服务器IP地址（如192.168.1.10）和端口（如8080）设置对话框，并实现服务器IP地址和端口的保存和修改功能。
 * ⑥显示“进入主页”按钮，点击进入主页面。
 */
@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity {
    private ViewPager vpSplash;
    //点
    private View v1;
    private View v2;
    private View v3;
    private View v4;
    private View v5;
    //图片
    private int[] images = new int[]{R.mipmap.splash1, R.mipmap.splash2, R.mipmap.splash3, R.mipmap.splash4, R.mipmap.splash5};
    //点
    private List<View> points;
    //1 viewpager当中使用fragment
    private List<Fragment> fragments;
    private SplashVpAdapter vpAdapter;

    //2 viewpager使用view代替fragment
//    private List<View> views;
//    private SplashVPAdapter2 splashVPAdapter2;
//    private Button btPort;
//    private Button btIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //拿到sp存储
        SPUtils spUtils = SPUtils.config(this, Constant.SP_NET_FILE_NAME);
        //若sp当中有ip及其端口就拼接并启动主页 若没有，启动引导页
        if (spUtils.hasSet()) {
            //拼接基本地址
            ApiConfig.BASE_URL += spUtils.getString(Constant.NET_PORT) + ":" + spUtils.getString(Constant.NET_IP);
            //拼接头像地址
            ApiConfig.BASE_AVATER = ApiConfig.BASE_URL + ApiConfig.MIDDLE_AVATER_URL;
            //跳转至主页
            migrateTo(HomeActivity.class);
            //结束当前活动
            destroyThis();
        } else {
            setContentView(R.layout.activity_splash);

            init();
            initEvent();
        }
    }

    public void init() {
        vpSplash = (ViewPager) findViewById(R.id.vp_splash);
        v1 = (View) findViewById(R.id.v1);
        v2 = (View) findViewById(R.id.v2);
        v3 = (View) findViewById(R.id.v3);
        v4 = (View) findViewById(R.id.v4);
        v5 = (View) findViewById(R.id.v5);
        fragments = new ArrayList<>();
        points = new ArrayList<>();

//        views = new ArrayList<>();
    }

    public void initEvent() {
        //5个点
        points.add(v1);
        points.add(v2);
        points.add(v3);
        points.add(v4);
        points.add(v5);
        //1到4页的fragment
        for (int i = 0; i < points.size() - 1; i++) {
            SplashVpFragment fragment = new SplashVpFragment();
            fragment.setImg(images[i]);
            fragments.add(fragment);

            /******************************  使用view  ******************************************/
//            View view = LayoutInflater.from(this).inflate(R.layout.fragment_splash_vp,null);
//            ImageView imageView = view.findViewById(R.id.iv_splash);
//            imageView.setImageResource(images[i]);
//            views.add(view);
        }

        //最后一页 进入页
        SplashLoginFragment fragment = new SplashLoginFragment();
        fragment.setImg(images[4]);
        fragments.add(fragment);

        /******************************    使用view    ******************************************/
//        View view = LayoutInflater.from(this).inflate(R.layout.fragment_splash_login,null);
//        ImageView imageView = view.findViewById(R.id.iv_splash_login);
//        imageView.setImageResource(images[4]);
//        btPort = (Button) view.findViewById(R.id.bt_port);
//        btIn = (Button) view.findViewById(R.id.bt_in);
//        views.add(view);

        vpAdapter = new SplashVpAdapter(getSupportFragmentManager(), 0, fragments, this);
        vpSplash.setAdapter(vpAdapter);

        /**********************************    使用view    **************************************/
//        splashVPAdapter2 = new SplashVPAdapter2(this,views);
//        vpSplash.setAdapter(splashVPAdapter2);
//        btPort.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NetSetDialog dialog = new NetSetDialog(SplashActivity.this);
//                dialog.show();
//            }
//        });
//        btIn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(SplashActivity.this, HomeActivity.class));
//            }
//        });

        //vp监听
        vpSplash.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @SuppressLint("UseCompatLoadingForDrawables")
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //vp滑动完毕 重新设置点的样式，全部设置成默认的之后再设置单个变化的点 虽然不优雅但是简单直接
                for (int i = 0; i < points.size(); i++) {
                    points.get(i).setBackground(getDrawable(R.drawable.shape_splash_point_common));
                }
                points.get(position).setBackground(getDrawable(R.drawable.shape_splash_round_point));
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}